/*     */ package com.zimbra.cs.index.analysis;
/*     */ 
/*     */ import com.google.common.base.CharMatcher;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.index.LuceneIndex;
/*     */ import java.io.IOException;
/*     */ import java.io.Reader;
/*     */ import java.util.Set;
/*     */ import org.apache.lucene.analysis.Analyzer;
/*     */ import org.apache.lucene.analysis.CharReader;
/*     */ import org.apache.lucene.analysis.StopAnalyzer;
/*     */ import org.apache.lucene.analysis.StopFilter;
/*     */ import org.apache.lucene.analysis.TokenFilter;
/*     */ import org.apache.lucene.analysis.TokenStream;
/*     */ import org.apache.lucene.analysis.Tokenizer;
/*     */ import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
/*     */ import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class UniversalAnalyzer
/*     */   extends Analyzer
/*     */ {
/*     */   private Tokenizer savedTokenizer;
/*     */   private TokenStream savedTokenStream;
/*     */   
/*     */   public TokenStream tokenStream(String field, Reader in)
/*     */   {
/*  57 */     return createTokenStream(createTokenizer(in));
/*     */   }
/*     */   
/*     */ 
/*     */   public final TokenStream reusableTokenStream(String field, Reader in)
/*     */     throws IOException
/*     */   {
/*  64 */     if ((this.savedTokenizer != null) && (this.savedTokenStream != null)) {
/*  65 */       this.savedTokenizer.reset(new NormalizeTokenFilter(CharReader.get(in)));
/*     */     } else {
/*  67 */       this.savedTokenizer = createTokenizer(in);
/*  68 */       this.savedTokenStream = createTokenStream(this.savedTokenizer);
/*     */     }
/*  70 */     return this.savedTokenStream;
/*     */   }
/*     */   
/*     */   private Tokenizer createTokenizer(Reader in) {
/*  74 */     return new UniversalTokenizer(new NormalizeTokenFilter(CharReader.get(in)));
/*     */   }
/*     */   
/*     */   private TokenStream createTokenStream(Tokenizer tokenizer) {
/*  78 */     TokenStream result = new UniversalTokenFilter(tokenizer);
/*  79 */     Set stopWords = StopAnalyzer.ENGLISH_STOP_WORDS_SET;
/*     */     try {
/*  81 */       stopWords = Provisioning.getInstance().getConfig().getMultiAttrSet("zimbraDefaultAnalyzerStopWords");
/*     */     } catch (ServiceException e) {
/*  83 */       ZimbraLog.index.error("Failed to retrieve stop words from LDAP", e);
/*     */     }
/*     */     
/*  86 */     result = new StopFilter(LuceneIndex.VERSION, result, stopWords);
/*  87 */     return result;
/*     */   }
/*     */   
/*     */   private static class UniversalTokenFilter extends TokenFilter {
/*  91 */     private CharTermAttribute termAttr = (CharTermAttribute)addAttribute(CharTermAttribute.class);
/*  92 */     private TypeAttribute typeAttr = (TypeAttribute)addAttribute(TypeAttribute.class);
/*     */     
/*     */     UniversalTokenFilter(TokenStream in) {
/*  95 */       super();
/*     */     }
/*     */     
/*     */     public boolean incrementToken() throws IOException
/*     */     {
/* 100 */       if (!this.input.incrementToken()) {
/* 101 */         return false;
/*     */       }
/*     */       
/* 104 */       String type = this.typeAttr.type();
/* 105 */       if (type == UniversalTokenizer.TokenType.APOSTROPHE.name())
/*     */       {
/* 107 */         int len = this.termAttr.length();
/* 108 */         if ((len >= 2) && (this.termAttr.charAt(len - 1) == 's') && (this.termAttr.charAt(len - 2) == '\''))
/*     */         {
/* 110 */           this.termAttr.setLength(len - 2);
/*     */         }
/* 112 */       } else if (type == UniversalTokenizer.TokenType.ACRONYM.name())
/*     */       {
/* 114 */         String replace = CharMatcher.is('.').removeFrom(this.termAttr);
/* 115 */         this.termAttr.setEmpty().append(replace);
/*     */       }
/*     */       
/* 118 */       return true;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/analysis/UniversalAnalyzer.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */